package com.dgut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgut.entity.Deliverynotes;
import com.dgut.entity.SaleOrders;
import com.dgut.entity.Salescontracts;
import com.dgut.mapper.SalesOrdersMapper;
import com.dgut.mapper.SalescontractsMapper;
import com.dgut.service.DeliverynotesService;
import com.dgut.service.TurnoverService;
import com.dgut.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class TurnoverServiceImpl implements TurnoverService {
    @Autowired
    private DeliverynotesService deliverynotesService;
    @Autowired
    private SalesOrdersMapper salesOrdersMapper;
    @Autowired
    private SalescontractsMapper salescontractsMapper;

    @Override
    public TurnoverVO getTurnover(LocalDate startDate, LocalDate endDate) {
        //时间装换
        LocalDateTime start = startDate.atStartOfDay();
        LocalDateTime end = endDate.atStartOfDay();

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.between("create_time", start, end);
        List<Deliverynotes> list = deliverynotesService.list(queryWrapper);

        TurnoverVO turnoverVO = new TurnoverVO();
        System.out.println(turnoverVO);

        // 计算营业额
        //根据订单id查询订单信息
        if (list.size() != 0) {
            for (Deliverynotes deliverynote : list) {
                QueryWrapper queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("order_id", deliverynote.getOrderId());
                List<SaleOrders> salesOrder = salesOrdersMapper.selectList(queryWrapper1);

                if (salesOrder.size() != 0 && salesOrder != null) {
                    salesOrder.get(0).getPrice();
                    turnoverVO.setTotalSales(turnoverVO.getTotalSales() + salesOrder.get(0).getPrice() * deliverynote.getQuantityShipped());

                    //查询对应的合同
                    Salescontracts salescontract = salescontractsMapper.selectById(salesOrder.get(0).getContractId());

                    // 查询客户是否已存在salesByCustomers中
                    List<SalesByCustomer> salesByCustomers = turnoverVO.getSalesByCustomers();
                    int flag = 0;
                    for (SalesByCustomer salesByCustomer : salesByCustomers) {
                        if (salesByCustomer.getCusName().equals(salescontract.getCusName())) {
                            salesByCustomer.setSales(salesByCustomer.getSales() + salesOrder.get(0).getPrice() * deliverynote.getQuantityShipped());
                            flag = 1;
                            break;
                        }
                    }
                    // 如果不在就添加
                    if (flag == 0) {
                        SalesByCustomer salesByCustomer = new SalesByCustomer(salescontract.getCusName(),
                                salesOrder.get(0).getPrice() * deliverynote.getQuantityShipped());
                        turnoverVO.getSalesByCustomers().add(salesByCustomer);
                    }

                    List<SalesByProduct> salesByProducts = turnoverVO.getSalesByProducts();
                    flag = 0;
                    for (SalesByProduct salesByProduct : salesByProducts) {
                        if (salesByProduct.getProductName().equals(salesOrder.get(0).getProductName())) {
                            salesByProduct.setSales(salesByProduct.getSales() + salesOrder.get(0).getPrice() * deliverynote.getQuantityShipped());
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 0) {
                        SalesByProduct salesByProduct = new SalesByProduct(salesOrder.get(0).getProductName(),
                                salesOrder.get(0).getPrice() * deliverynote.getQuantityShipped());
                        turnoverVO.getSalesByProducts().add(salesByProduct);
                    }
                }
            }
        }
        return turnoverVO;
    }

    @Override
    public IndividualSalesVO getTurnoverByEmpId(Long empId, LocalDate startDate, LocalDate endDate) {
        // 根据empId 查询销售合同
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("emp_id", empId);
        queryWrapper.between("contract_date", startDate, endDate);
        List<Salescontracts> list = salescontractsMapper.selectList(queryWrapper);

        IndividualSalesVO individualSalesVO = new IndividualSalesVO();

        //计算营业额
        for (Salescontracts salescontract : list) {
            SingleTurnover singleTurnover = new SingleTurnover();
            BeanUtils.copyProperties(salescontract, singleTurnover);

            // 根据合同id查询订单
            long contractId = salescontract.getContractId();
            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("contract_id", contractId);
            List<SaleOrders> list1 = salesOrdersMapper.selectList(queryWrapper1);

            for (SaleOrders order : list1) {

                singleTurnover.setContractAmount(singleTurnover.getContractAmount() + order.getPrice() * order.getQuantity());
                singleTurnover.setExecutedAmount(singleTurnover.getExecutedAmount() + order.getPrice() * order.getPayedCount());
            }
            individualSalesVO.getContractsTurnovers().add(singleTurnover);
            individualSalesVO.setTotal(individualSalesVO.getTotal() + singleTurnover.getContractAmount());
        }

        return individualSalesVO;
    }
}
